package com.infoDiffusion.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.xml.utils.StringToIntTable;
import org.springframework.beans.factory.annotation.Autowired;

import com.infoDiffusion.bean.KnowledgeBean;
import com.infoDiffusion.bean.UserBean;
import com.infoDiffusion.service.AdminService;
import com.infoDiffusion.util.Constants;
import com.infoDiffusion.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AdminAction extends ActionSupport {
	@Autowired
	AdminService adminService;
	UserBean userBean;
	UserBean bean;
	public HttpServletResponse response = ServletActionContext.getResponse();
	public HttpServletRequest request = ServletActionContext.getRequest();
	public HttpSession session = request.getSession();

	// 注销
	public String logout() {
		request.getSession().removeAttribute(Constants.SESSION_ADMIN);
		return "logout";
	}

	// 登录
	public String login() {
		bean = adminService.login(userBean);
		if (bean != null) {
			session.setAttribute(Constants.SESSION_ADMIN, bean);
			// System.out.println(bean.getId());
			int power = bean.getPower();
			if (power == 1) {
				return "login1";
			} else if (power == 2) {
				return "login2";
			} else if (power == 3) {
				return "login3";
			} else if (power == 4) {
				return "login4";
			} else if (power == 0) {
				return "login0";
			}
		}
		return "loginfailure";
	}

	// 删除用户
	public String delete() {
		int id = StringUtil.toInt(request.getParameter("id"));
		if (id > 0) {
			UserBean admin = adminService.getById(id);
			boolean flag = adminService.delete(admin);
			if (flag)
				return "delete";
		}
		return "flase";
	}

	// 修改用户
	public String toUpdate() {
		int id = StringUtil.toInt(request.getParameter("id"));
		if (id > 0) {
			UserBean bean = adminService.getById(id);
			ActionContext.getContext().put("uBean", bean);
		}
		return "toUpdate";
	}

	public String update() {
		boolean flag = adminService.update(bean);
		if (flag) {
			return "update";
		}
		return "updatefalse";
	}

	// 获取个人信息
	public String toGetInfo() {
		bean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		int id = bean.getId();
		if (id > 0) {
			UserBean bean = adminService.getById(id);
			ActionContext.getContext().put("bean", bean);
		}
		return "toGetInfo";
	}

	// 修改密码
	public String toAlterPassword() {
		bean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		int id = bean.getId();
		if (id > 0) {
			UserBean bean = adminService.getById(id);
			ActionContext.getContext().put("bean", bean);
		}
		return "alterPassword";
	}

	public String alterPassword() {
		bean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		String password = request.getParameter("password1");

		bean.setPassword(password);
		boolean flag = adminService.updatePass(bean);
		if (flag) {
			return "toGetInfo";
		}
		return "false";
	}

	public void checkpwd() {
		int id = Integer.parseInt(request.getParameter("id"));
		UserBean bean = adminService.getById(id);
		String password = request.getParameter("password");
		try {
			PrintWriter out = response.getWriter();
			if (password.equals(bean.getPassword())) {
				out.write("true");
			} else {
				out.write("false");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String add() {
		boolean b = adminService.add(userBean);
		if (b) {
			return "add";
		}
		return "false";
	}

	// 用户列表
	public String list() {
		List<UserBean> list = adminService.list(userBean);
		ActionContext.getContext().put("list", list);
		return "listUser";
	}

	public void checkUsername() {
		String username = request.getParameter("username");
		UserBean bean = adminService.getByUsername(username);
		boolean flag = true;
		if (bean != null) {
			flag = false;
		}
		try {
			PrintWriter out = response.getWriter();
			out.print(flag);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public UserBean getBean() {
		return bean;
	}

	public void setBean(UserBean bean) {
		this.bean = bean;
	}

}
